home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / sbin / ekey-rekey < prev    next >
Encoding:
Text File  |  2011-08-10  |  3.9 KB  |  171 lines

  1. #!/bin/sh
  2.  
  3. if test "x$KEYRING" = "x"; then
  4.     KEYRING=/etc/entropykey/keyring
  5. fi
  6.  
  7. usage() {
  8.     cat <<EOF >&2
  9. ekey-rekey: Utility to re-key an Entropy Key's Long-term-key
  10. Usage:
  11.     ekey-rekey [-d|--device DEVICENODE] [SERIAL [MASTERKEY]]
  12. EOF
  13. }
  14.  
  15. # settings
  16. DEVDIR=/sys/bus/usb/devices;
  17.  
  18. # find an ekey by searching sysfs
  19. sysfs_find_ekey () {
  20.  
  21.     for USBDEV in $(ls ${DEVDIR});do 
  22.     USBDEVDIR=${DEVDIR}/${USBDEV}; 
  23.     if [ -f "${USBDEVDIR}/idVendor" -a -f "${USBDEVDIR}/idProduct" -a -f "${USBDEVDIR}/serial" ];then 
  24.         if [ "$(cat ${USBDEVDIR}/idVendor)" = "20df" -a "$(cat ${USBDEVDIR}/idProduct)" = "0001" ]; then
  25.         # check the serial number either for not being specified or it matches given value
  26.         if [ "x$1" = "x" -o "$(cat ${USBDEVDIR}/serial)" = "$1" ]; then
  27.             echo ${USBDEV} $(cat ${USBDEVDIR}/serial)
  28.             break;
  29.         fi
  30.         fi 
  31.     fi
  32.     done
  33. }
  34.  
  35. sysfs_find_ekey_dev () {
  36.     SYSFS_DEV_MAJ_MIN_FILE=${DEVDIR}/${1}:1.0/tty/tty*/dev
  37.     if [ -f ${SYSFS_DEV_MAJ_MIN_FILE} ]; then
  38.     SYSFS_DEV_MAJ_MIN=$(cat ${SYSFS_DEV_MAJ_MIN_FILE})
  39.     DEV_MAJ=$(echo ${SYSFS_DEV_MAJ_MIN} | cut -d : -f1)
  40.     DEV_MIN=$(echo ${SYSFS_DEV_MAJ_MIN} | cut -d : -f2)
  41.  
  42.     find /dev -type c | xargs stat -c "%t:%T %n" | egrep "^$(printf "%x" ${DEV_MAJ}):$(printf "%x" ${DEV_MIN})" | cut -d ' ' -f2
  43.  
  44.     fi
  45. }
  46.  
  47. if test $# -ge 1; then
  48.  
  49. optloop=1
  50. while test ${optloop} = 1; do
  51.     case x$1 in
  52.     x-d|x--device)
  53.     DEVICE=$2
  54.     shift
  55.     shift
  56.     ;;
  57.     x--device=*)
  58.     DEVICE=$(echo "x$1" | sed 's/^x--device=//')
  59.     shift
  60.     ;;
  61.     x-h|x-\?|x--help)
  62.     usage
  63.     exit 0
  64.     ;;
  65.     x-V|x--version)
  66.     echo "ekey-rekey version ""1.1.4"
  67.     exit 0
  68.     ;;
  69.     x--)
  70.     shift
  71.     break
  72.     ;;
  73.     x|x-*)
  74.     # empty or unknown -o or unknown --long-option
  75.     usage
  76.     exit 1
  77.     ;;
  78.     *)
  79.     # non-option argument
  80.     optloop=0
  81.     ;;
  82.     esac
  83. done
  84.  
  85. fi
  86.  
  87. # first open parameter must be the serial number
  88. SERIAL=$1
  89.  
  90. if [ "x${SERIAL}" = "x" ]; then
  91.     # no serial found go looking for the user
  92.     FOUND_EKEY=$(sysfs_find_ekey)
  93.  
  94.     if [ "x${FOUND_EKEY}" = "x" ];then
  95.     echo >&2 "No Entropy Key could be found. Is it connected?"
  96.     exit 2
  97.     fi
  98.     FOUND_EKEY_DEV=$(sysfs_find_ekey_dev ${FOUND_EKEY})
  99.  
  100.     SERIAL=$(echo ${FOUND_EKEY} | cut -d ' ' -f2)
  101.     
  102.     echo "Found Entropy Key Serial: ${SERIAL}"
  103. else
  104.     # user supplied serial number
  105.  
  106.     echo "Specified Entropy Key Serial: ${SERIAL}"
  107.  
  108.     #look for appropriate device node just in case
  109.     FOUND_EKEY=$(sysfs_find_ekey ${SERIAL})
  110.     if [ "x${FOUND_EKEY}" != "x" ];then
  111.     FOUND_EKEY_DEV=$(sysfs_find_ekey_dev ${FOUND_EKEY})
  112.     fi
  113.  
  114.     shift
  115.     MASTERKEY=$(echo $@ | tr -d ' ')
  116. fi
  117.  
  118. # alter the serial number to ensure it contains no path separators
  119. SERIALD="$(echo "${SERIAL}" | tr / .)"
  120. SERIALU="$(echo "${SERIAL}" | tr / _)"
  121.  
  122. DEVPATHS=""
  123.  
  124. if test "x" != "x$DEVICE"; then
  125.     DEVPATHS="$DEVICE "
  126. fi
  127.  
  128. DEVPATHS="${DEVPATHS}/dev/entropykey/${SERIALD} /dev/entropykey/${SERIALU} /var/run/entropykeys/${SERIALD} /var/run/entropykeys/${SERIALU} ${FOUND_EKEY_DEV}"
  129.  
  130. for DEVPATH in ${DEVPATHS}; do
  131.     if test -c "${DEVPATH}" -o -h "${DEVPATH}" -o -p "${DEVPATH}" -o -S "${DEVPATH}"; then
  132.         test "x${NODETOUSE}" = "x" && NODETOUSE="${DEVPATH}"
  133.     fi
  134. done
  135.  
  136. if test "x" = "x$NODETOUSE"; then
  137.     echo >&2 "Unable to find any device node or socket for $SERIAL"
  138.     echo >&2 "Looked in: $DEVPATHS"
  139.     exit 2
  140. fi
  141.  
  142. ctl() {
  143.     ekeydctl "$@" 2>/dev/null
  144. }
  145.  
  146.  
  147. # Try to ensure that any running daemon ignores the key
  148. ctl remove "$SERIAL"
  149. test $? = 4 && {
  150.     echo >&2 "Warning: Could not detach key from daemon."
  151.     echo >&2 "         If there is a problem setting the long-term key try"
  152.     echo >&2 "         stopping the daemon before re-running the rekey tool."
  153. }
  154.  
  155. # Generate the new key
  156. if [ "x${MASTERKEY}" = "x" ];then
  157.     ekey-setkey -s "$SERIAL" -f "$KEYRING" "$NODETOUSE"
  158. else
  159.     ekey-setkey -s "$SERIAL" -m "$MASTERKEY" -f "$KEYRING" "$NODETOUSE"
  160. fi
  161.  
  162. if test $? -ne 0; then
  163.     echo >&2 "Unable to generate new long-term key"
  164. fi
  165.  
  166. # Re-add the new keyring
  167. ctl keyring "$KEYRING"
  168.  
  169. # Add the new key and hope for the best
  170. ctl add "$NODETOUSE"
  171.